// Vuex 状态管理
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
	// 数据源
	state: {
		// historyLists: []
		historyLists: uni.getStorageSync('__history')|| [],//持久化，放到了本地缓存里
	},
	mutations: {
		SET_HISTORY_LISTS(state, history) {
			state.historyLists = history;
		}
		,
		CLEAR_HISTORY(state){
			state.historyLists = [];
		}
	},
	actions: {
		set_history({
			commit,
			state
		}, history) {
			let list = state.historyLists;
			list.unshift(history);
			uni.setStorageSync('__history',list);
			// ！！调用 mutations 里面的方法
			commit('SET_HISTORY_LISTS',list);
		},
		clear_history({commit}){
			// uni.setStorageSync('__history',[]);
			uni.removeStorageSync('__history');
			commit('CLEAR_HISTORY');
			
		}
		
	}
})

export default store;
